package com.stickerview.view;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import com.qozix.tileview.R;
import com.stickerview.core.IMGPath;

import java.io.File;
import java.io.InputStream;

/**
 * Created by felix on 2017/11/14 下午7:27.
 */
public class StickerImageView extends StickerView {

    private static final String TAG = "IMGStickerTextView";

    private ImageView mTextView;
    private AssetManager assetManager ;
    private IMGPath mPath;
    private int hasFilp = 0 ;

    private static float mBaseTextSize = -1f;

    private static final int PADDING = 26;

    private static final float TEXT_SIZE_SP = 24f;

    public StickerImageView(Context context) {
        this(context, null, 0);
    }

    public StickerImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StickerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onInitialize(Context context) {
        if (mBaseTextSize <= 0) {
            mBaseTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                    TEXT_SIZE_SP, context.getResources().getDisplayMetrics());
        }
        super.onInitialize(context);
    }

    @Override
    public View onCreateContentView(Context context) {
        mTextView = new ImageView(context);
        mTextView.setPadding(PADDING, PADDING, PADDING, PADDING);
        assetManager=getContext().getAssets();
        return mTextView;
    }

    public void setText(IMGPath text){
        setText(text,1,1);
    }

    /**
     * 图片地址   是否翻转
     * @param text
     * @param hasFilp
     */
    public void setText(IMGPath text,int hasFilp,int isLock) {
        this.hasFilp = hasFilp;
        this.mPath = text;
        if (mTextView != null) {
            String path = text.getPath();
            if(path==null){
                mTextView.setImageResource(R.drawable.marker);
                mTextView.setTag(R.id.tag_first,0);
                mTextView.setTag(R.id.tag_second,hasFilp);
            }else{
                if(path.contains("android_asset/")){
                    String[] p = path.split("android_asset/");
                    path = p[1];
                    try {
                        InputStream in=assetManager.open(path);
                        Bitmap bmp=BitmapFactory.decodeStream(in);
                        mTextView.setImageBitmap(bmp);
                        mTextView.setTag(R.id.tag_first,path);
                        mTextView.setTag(R.id.tag_second,hasFilp);
                    } catch (Exception e) {
                        mTextView.setImageResource(R.drawable.marker);
                        mTextView.setTag(R.id.tag_first,0);
                        mTextView.setTag(R.id.tag_second,hasFilp);
                    }
                }else {
                    File file = new File(path);
                    if(file.exists()){
                        Uri uri = Uri.fromFile(file);
                        mTextView.setImageURI(uri);
                        mTextView.setTag(R.id.tag_first,path);
                        mTextView.setTag(R.id.tag_second,hasFilp);
                    }
                }
            }
        }
        if(hasFilp==0){
            super.changeFlip();
        }
        if(isLock==0){
            super.lock();
        }
    }


    public IMGPath getPath() {
        return mPath;
    }

    @Override
    public void onContentTap() {
        //IMGTextEditDialog dialog = getDialog();
        //dialog.setText(mPath);
        //dialog.show();
    }
}